#!/bin/bash

# Slackware build script for qutebrowser-bin

# Copyright 2019-2023, Alexander Verbovetsky, Moscow, Russia
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=qutebrowser-bin
VERSION=${VERSION:-2.5.2}
BUILD=${BUILD:-3}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ "$ARCH" != "x86_64" ]; then
  echo "Only x86_64 is supported."
  exit 1
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

set -e

rm -rf $PKG
mkdir -p $TMP $PKG/opt $OUTPUT
cd $PKG/opt
tar xvf $CWD/qutebrowser-$VERSION.tar.gz
mv qutebrowser-$VERSION qutebrowser
cd qutebrowser
sed -i.bak "s|\(run_venv(venv_dir, 'python', '-m', 'pip', 'install',\)|\1 \
'--no-index', '--find-links=$CWD/',|" scripts/mkvenv.py

sed -i "/env\['PATH'\] = str(bin_path) + os.pathsep + env\['PATH'\]/d" \
  scripts/asciidoc2html.py

chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

python3 scripts/mkvenv.py --skip-smoke-test

mv scripts/mkvenv.py.bak scripts/mkvenv.py

find ./ -type f -name '*.pyc' -delete
find ./ -type d -name '__pycache__' -delete
find ./ -type f -exec sed -i -e "s|$PKG||" {} \;
find ./ -type f -exec sed -i -e \
  '1,2 s|^#!/usr/bin/env python.*$|#!/opt/qutebrowser/.venv/bin/python3|' {} \;
.venv/bin/python3 -m compileall scripts/__init__.py \
  scripts/setupcommon.py scripts/utils.py -fq
.venv/bin/python3 -m compileall .venv/lib -fq

.venv/bin/python3 scripts/asciidoc2html.py
a2x -f manpage doc/qutebrowser.1.asciidoc
gzip -9 doc/qutebrowser.1
mkdir -p $PKG/usr/man/man1
mv doc/qutebrowser.1.gz $PKG/usr/man/man1

mkdir -p $PKG/usr/share/{metainfo,applications,icons/hicolor/scalable/apps,pixmaps,qutebrowser/pdf.js}
install -m 0644 misc/org.qutebrowser.qutebrowser.appdata.xml \
  $PKG/usr/share/metainfo/
install -m 0644 misc/org.qutebrowser.qutebrowser.desktop \
  $PKG/usr/share/applications
for res in 16 24 32 48 64 96 128 256 512; do
  mkdir -p $PKG/usr/share/icons/hicolor/${res}x${res}/apps/;
  install -m 0644 "icons/qutebrowser-${res}x${res}.png" \
"$PKG/usr/share/icons/hicolor/${res}x${res}/apps/qutebrowser.png"
done
install -m 0644 "icons/qutebrowser.svg" \
  $PKG/usr/share/icons/hicolor/scalable/apps/
install -m 0644 "icons/qutebrowser.xpm" $PKG/usr/share/pixmaps/

cp -a misc/userscripts $PKG/usr/share/qutebrowser
cp -a scripts $PKG/usr/share/qutebrowser
rm -rf $PKG/usr/share/qutebrowser/scripts/{__pycache__,dev,testbrowser,__init__.py,asciidoc2html.py,setupcommon.py,link_pyqt.py}

mkdir -p $PKG/usr/bin
install -m 0755 -o root -g root $CWD/wrapper.qutebrowser \
  $PKG/usr/bin/qutebrowser

find ./ -type f -exec sed -i -e \
  's|/usr/share/pdf.js|/usr/share/qutebrowser/pdf.js|' {} \;
cd $PKG/usr/share/qutebrowser/pdf.js
unzip $CWD/pdfjs-*-dist.zip
chown -R root:root $PKG/usr/share/qutebrowser/pdf.js
find ./ -type f -exec chmod 644 {} \;

find $PKG -print0 | xargs -0 file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
mv $PKG/opt/qutebrowser/README.asciidoc $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
